<template>
  <div class="container bg-white">
    <blank
      :value="showEmpty"
      :title="empty.title"
      :message="empty.message"
      :icon="empty.icon"
    >
      <template slot="button">
        <van-button
          icon="home-o"
          round
          :to="{path:'/'}"
          replace
        >返回首页</van-button>
      </template>
    </blank>
  </div>
</template>

<script>
import { Tag, Col, Icon, Button, Loading, Toast } from 'vant'
import Blank from '@/components/abnor/blank'
import { parseShareData } from '@/api/modules/global'
import { setSpread } from '@/utils/auth'
export default {
  components: {
    Blank,
    [Tag.name]: Tag,
    [Col.name]: Col,
    [Icon.name]: Icon,
    [Button.name]: Button,
    [Loading.name]: Loading
  },
  async beforeRouteEnter (to, from, next) {
    console.group('beforeRouteEnter')
    // 先禁止进入
    next(false)
    let loadingInstance = Toast({
      message: '优越定制',
      icon: 'http://attach.youyuegw.com/assets/brand/app.png',
      overlay: false,
      duration: 0,
      position: 'middle',
      className: 'boot-page',
      forbidClick: true
    })

    let redirectUrl = window.location.origin + '/#'

    setTimeout(function () {
      parseShareData(to.params).then(({ data }) => {
        console.log('[share api]', data)
        // 保存推广用户信息
        if (data.spread) {
          setSpread(data.spread)
        }
        if (data.type) {
          console.log('[go type]', data.type)
          if (data.type === 'goods' && data.id) {
            window.location.href = redirectUrl + '/goods/' + data.id
          } else {
            window.location.href = redirectUrl + '/'
          }
        } else {
          console.log('[gohome]', true)
          window.location.href = redirectUrl + '/'
        }
        console.log('test loadingInstance')
        loadingInstance !== null && loadingInstance.clear()
      }).catch(e => {
        next(vm => {
          vm.setEmpty()
        })
      })
    }, 1000)
    console.groupEnd('beforeRouteEnter')
  },
  data () {
    return {
      showEmpty: false,
      empty: {
        title: '分享内容不存在',
        message: '抱歉，当前内容不存在或者已删除',
        icon: '/static/icons/blank_authent.png'
      }
    }
  },
  methods: {
    setEmpty () {
      this.showEmpty = true
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.fullscreen {
  height: 100%;
  overflow: hidden;
  position: relative;
  background: #fff;
  .container {
    height: 100%;
  }
}
.van-loading {
  height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.van-popup {
  h1 {
    margin: 0;
    font-size: 14px;
    padding: 15px;
    border-bottom: 1px solid #f2f2f2;
  }
}
</style>
